<?php
declare (strict_types=1);

namespace app\api\controller;

use app\common\model\Area;
use app\common\model\Evaluate;
use app\common\model\HistorySearch;
use app\common\model\Order as OrderModel;
use app\common\model\ProductCate;
use app\common\model\User;
use app\common\tool\Result;
use GuzzleHttp\HandlerStack;
use think\facade\Cache;
use think\facade\View;
use think\response\Json;
use app\common\model\Product as ProductModel;

class Product extends Base
{




    /**
     * get: 商品分类
     * path: cate
     * method: cate
     * param: pid - {string} 上级 一级分类传0
     */
    public function cate(): \think\Response
    {
        $param = $this->request->param();
        $cate = ProductCate::where(['status' => 1, 'is_del' => 0, 'pid' => $param['pid']])->order('sort','asc')->field('id,title')->select();
        if (isset($cate)) {
            return Result::success('获取成功', $cate);
        } else {
            return Result::fail('获取失败');
        }
    }


    /**
     * get: 商品列表
     * path: ProductList
     * method: ProductList
     * param: keyword - {string} 搜索关键词
     * param: page - {string} 页数
     * param: cate_1 - {string} 一级分类
     * param: cate_2 - {string} 二级分类
     * param: keyword - {string} keyword
     */
    public function ProductList(): \think\Response
    {
        try {
            $param = $this->request->param();
            $that = ProductModel::field('id,title,price,vip_price,icon');
            $map = [];
            if (isset($param['cate_1'])) {
                $area = explode(',', $param['cate_1']);
                $that->wherein('cate_1', $area);
                unset($param['cate_1']);
            }
            if (isset($param['cate_2'])) {
                $area = explode(',', $param['cate_2']);
                $that->wherein('cate_2', $area);
                unset($param['cate_2']);
            }
            if (isset($param['keyword'])) {
                $that->whereLike('title', '%' . $param['keyword'] . '%');
                $token = $this->request->header('token');
                if ($token) {
                    HistorySearch::setHistory($param['keyword'], $this->user->id);
                }
            }
            $that->where($map);
            $that->where(['status' => 1, 'is_del' => 0]);

            $that->order('sort', 'asc')->order('create_time', 'desc');
            $that->page(intval($param['page'] ?? 1), 20);
            $list = $that->select();
            if (isset($list)) {
                return Result::success('获取成功', $list);
            } else {
                return Result::fail('获取失败');
            }
        } catch (\Exception $e) {
            return Result::success('获取成功', $e->getMessage());
        }
    }


    /**
     * get: 商品详情
     * path: details
     * method: details
     * param: id - {string} id
     */
    public function details()
    {
        $id = $this->request->param('id');
        $info = ProductModel::where('id', $id)->field('id,title,cate_1,price,vip_price,content,images')->find();
        $info->content = handleRichText($info->content);
        $info->images = explode(',', $info->images);
        if (isset($info)) {
            return Result::success('获取成功', $info);
        } else {
            return Result::fail('获取失败');
        }
    }



    /**
     * post: 购买商品
     * path: submit
     * method: submit
     * param: id - {string} id
     * param: num - {string} 数量
     */
    public function submit()
    {
        $param = $this->request->post();
        if ($this->user->is_lock) {
            return Result::fail('账号已锁定');
        }
//        $num = \app\common\model\Address::where(['uid'=>$this->user->id,'default'=>1])->count();
//        if(!$num)
//        {
//            return Result::fail('请先设置默认地址');
//        }
        $submit = ProductModel::submit($param, $this->user);
        if ($submit['code'] > 0) {
            return Result::success($submit['msg']);
        } else {
            return Result::fail($submit['msg']);
        }
    }





}

